package com.aipp.mall.h5.service;

import com.aipp.mall.common.redis.RedisCache;
import com.aipp.mall.common.utils.Constants;
import com.aipp.mall.h5.domain.SysConfig;
import com.aipp.mall.h5.mapper.H5Mapper;
import jakarta.annotation.Resource;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.springframework.stereotype.Service;

/**
 * SysConfigService
 *
 * @author aa
 * @version 1.0
 * @since 2024/9/3
 */
@Service
public class SysConfigService {

    @Resource
    private RedisCache redisCache;

    @Resource
    private H5Mapper h5Mapper;

    public String selectConfigByKey(String configKey) {
        String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(configKey)));
        if (StrUtil.isNotEmpty(configValue)) {
            return configValue;
        }
        SysConfig config = new SysConfig();
        config.setConfigKey(configKey);
        SysConfig retConfig = h5Mapper.selectConfig(config);
        if (ObjUtil.isNotNull(retConfig)) {
            redisCache.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
            return retConfig.getConfigValue();
        }
        return "";
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey) {
        return Constants.SYS_CONFIG_KEY + configKey;
    }
}
